<?php

class SCILABExecutor {
    //private $setter;
    private $SCILABPath; //Ruta del programa SCILAB
    private $scriptsSCIPath; //Ruta del directorio de scripts
    private $scriptsAvailable;
    
    public function __construct($SCILABPath="/Applications/scilab-5.3.1.app/Contents/MacOS/bin/scilab", $scriptsSCIPath="/Applications/XAMPP/xamppfiles/htdocs/engineerplus/Calculator/Data/") {
        //$this->setter=new Setter($configurationsPath="../Config");
        $this->SCILABPath=$SCILABPath;
        $this->scriptsSCIPath=$scriptsSCIPath;
        $this->scriptsAvailable=array();
    }
    public function exe($scriptSCEName="script.sci",$fun="p(2)"){
        exec($this->SCILABPath.' -nwni -nb -e "exec(\"'.$this->scriptsSCIPath.$scriptSCEName.'\",0);disp('.$fun.');exit;"', $output);
        return $output;
    }
    public function getSCILABPath() {
        return $this->SCILABPath;
    }

    public function setSCILABPath($SCILABPath) {
        $this->SCILABPath = $SCILABPath;
    }

    public function getScriptsSCIPath() {
        return $this->scriptsSCIPath;
    }

    public function setScriptsSCIPath($scriptsSCIPath) {
        $this->scriptsSCIPath = $scriptsSCIPath;
    }
    
    public function getDescription(){
        $description=file("../Config/Calculator.html");
        foreach ($description as $line){
            $descriptionText.=trim($line,"\x00..\x1F");
        }
        return $descriptionText;
    }
    public function getScriptsList(){
        $scripts=scandir("../Data");
        //buscar .DS_Store (Servidores Mac)
        $a=array_search(".DS_Store", $scripts);
	if($a!=""){
            $this->scriptsAvailable = array_slice($scripts, 3);
        }
        else{
            $this->scriptsAvailable = array_slice($scripts, 2);
        }
	return $this->scriptsAvailable;
        
    }
    public function getScriptFunctionsList($scriptName="nombreScript.sci"){
        $script=file("../Data/".$scriptName);
        $functionsList=array();
        foreach ($script as $line){
            $aux=explode(" ", $line);
            if($aux[0]=="function"){
                $functionsList[]=$line;
            }
        }
        return $functionsList;
    }
    public function getFunctionsList(){
        $allFunctions=array();
        foreach ($this->getScriptsList() as $script){
            foreach ($this->getScriptFunctionsList($scriptName=$script) as $fun){
                $allFunctions[]=$script.":".$fun;
            }
            
        }
        return $allFunctions;
    }


}

?>
